package part21;

/**
 * @author wcj
 * @Date 2021/6/7 20:06
 * @Version 1.0
 */
public class Part2113 {

    /**
     * join：即让调用的线程先执行，执行返回之后再执行当前线程：可以让当前线程调用interrupt进行中断，但是中断的标识会清除
     *
     * @param args
     */
    public static void main(String[] args) {
        Sleeper sleepy = new Sleeper("Sleepy", 1500);
        Sleeper grumpy = new Sleeper("Grumpy", 1500);

        Joiner dopey = new Joiner("Dopey", sleepy);
        Joiner doc = new Joiner("Doc", grumpy);

        grumpy.interrupt();
    }
}

class Sleeper extends Thread {
    private int duration;

    public Sleeper(String name, int duration) {
        super(name);
        this.duration = duration;
        start();
    }

    @Override
    public void run() {
        try {
            sleep(duration);
        } catch (InterruptedException e) {
            System.out.println(this.getName() + " was interrupted." + "isInterrupted():" + this.isInterrupted());
            return;
        }
        System.out.println(getName() + " has awakened");
    }
}

class Joiner extends Thread {
    private Sleeper sleeper;

    public Joiner(String name, Sleeper sleeper) {
        super(name);
        this.sleeper = sleeper;
        start();
    }

    @Override
    public void run() {
        try {
            sleeper.join();
        } catch (InterruptedException e) {
            System.out.println(this.getName() + " was interrupted." + "isInterrupted():" + this.isInterrupted());
            return;
        }
        System.out.println(getName() + " join completed");
    }
}

